package com.hmkx.app.rest;


import com.hmkx.app.entity.Articles;
import com.hmkx.app.service.ArticleService;
import com.hmkx.app.util.cache.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(description = "文章相关",tags={"文章模块"})
public class ArticleController {

    Logger log = LoggerFactory.getLogger(ArticleController.class);

    @Resource
    private ArticleService articleService;
    @Resource(name="redisUtil")
    private RedisUtil redisUtil;

    @ApiOperation(value = "根据文章id获取文章数据",notes="注意事项")
    @ApiImplicitParam(name = "id",value = "文章id",dataType = "String",paramType = "query")
    @RequestMapping(value = "/getArticle",method = RequestMethod.GET)
    public String getArticleById(@RequestParam String id){
        log.info(id);
        Articles article = articleService.getArticleById(Integer.parseInt(id));
        if(article!=null) {
            redisUtil.set("aaaaa", article.getTitle());
            log.info("redis:get" + redisUtil.get("aaaaa"));
            log.info(article.getTitle());
            return article.getTitle();
        }
        return  "文章不存在";

    }


    @ApiOperation(value = "测试post请求",notes="注意事项")
    @ApiImplicitParam(dataType = "Articles",name = "user",value = "用户信息",required = true)
    @RequestMapping(value = "/testPost",method = RequestMethod.POST)
    public String testPost(@RequestBody Articles user){
        return "success";
    }


    @ApiOperation(value = "测试get请求",notes="注意事项")
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "String",paramType = "path")
    @RequestMapping(value = "/testGet/{id}",method = RequestMethod.GET)
    public String testGet(@PathVariable String id){
        return id;
    }

    @ApiOperation(value = "测试组合注解",notes="注意事项")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "User",name = "user",value = "用户信息",required = true,paramType = "body"),
            @ApiImplicitParam(dataType = "string",name = "id",value = "用户id",required = true,paramType = "path")
    })
    @RequestMapping(value = "/joinAnnotation/{id}",method = RequestMethod.POST)
    public Articles joinAnnotation(@PathVariable String id, @RequestBody Articles user){
        return user;
    }

    @ApiIgnore
    public String testIgnore(){
        return "success";
    }

}
